//
//  PersonVertifyController.m
//  ATMCash
//
//  Created by zhengchengfeng on 2024/9/11.
//

#import "PersonVertifyController.h"
#import "InPutTableViewCell.h"
#import "PickerThreeView.h"
#import "PickerCityView.h"

#import "IdentifyCardController.h"
#import "IDVertifyViewController.h"
#import "FaceViewController.h"
#import "IDVerfitySuccessController.h"
#import "IDVertifyModel.h"
#import "WorkInfoVertifyController.h"
#import "ContactVertifyController.h"
#import "BankVertifyController.h"


#define kTabCell  @"kTabCell"

#define kFootViewHeight  (15 + 48 + (IS_IPhoneX_All ? kHeight_BottomSafe : 15))

@interface PersonVertifyController ()

@end

@implementation PersonVertifyController

- (void)back{
    [self.view endEditing:YES];
    TipsAlertWLView *wl = [[TipsAlertWLView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    [[UIApplication sharedApplication].keyWindow addSubview:wl];
    wl.clickSureBlock = ^{
        [[self getCurrentVC] jumpToPopControlClass:@"OrderDetailViewController"];
    };
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}


- (void)initFootView{

    UIView *bottomV = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight - kFootViewHeight - kNavigationBarHeight, kScreenWidth, kFootViewHeight)];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    
    
    UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, 15, kScreenWidth - 30, 48)];
    nextBtn.titleLabel.font = [UIFont fontWithName:kFuturaMedium size:16];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateNormal];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateSelected];
    [nextBtn setTitleColor:[UIColor colorWithHexString:@"#FFEFC2"] forState:UIControlStateHighlighted];
    [nextBtn setTitle:@"Submit" forState:UIControlStateNormal];
    [nextBtn setTitle:@"Submit" forState:UIControlStateSelected];
    [nextBtn setTitle:@"Submit" forState:UIControlStateHighlighted];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateNormal];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateSelected];
    [nextBtn setBackgroundImage:[UIImage imageNamed:@"icon_idf4"] forState:UIControlStateHighlighted];
    [nextBtn addTarget:self action:@selector(clickSubmitBtn) forControlEvents:UIControlEventTouchUpInside];
    [bottomV addSubview:nextBtn];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Personal Information";
    
    [self loadData];
    
    [self.tableView registerClass:[InPutTableViewCell class] forCellReuseIdentifier:kTabCell];
    
}

- (void)initHeadView{
    UIImage *img = [UIImage imageNamed:@"icon_per1"];
    UIView *headV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, DimensionBaseIphone6(img.size.height))];
    self.tableView.tableHeaderView = headV;
    UIImageView *iconImgView = [[UIImageView alloc] initWithFrame:headV.frame];
    [headV addSubview:iconImgView];
    iconImgView.image = img;
}

- (void)loadData{
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    NSMutableDictionary *muDic = @{
        @"gardens":esString(_idStr),
    }.mutableCopy;
    [BaseDataEngine apiForPath:kVertifyPersonalUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        DLog(@"-----%@",responseDic);
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        
        self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kFootViewHeight -kNavigationBarHeight);
        [self initHeadView];
        [self initFootView];
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 20)];
        
        NSDictionary *dic = responseDic[@"recollecting"];
        NSArray *ary = dic[@"mob"];
       
        if (isArray(ary)) {
            self.data = [InPutModel mj_objectArrayWithKeyValuesArray:ary];
        }
        [self.tableView reloadData];
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    InPutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTabCell];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.data[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 105;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    InPutTableViewCell *selCell = [tableView cellForRowAtIndexPath:indexPath];
    InPutModel *mm  = selCell.model;
    
    if ([mm.weak isEqualToString:kMakemoneyk]) {
        PickerThreeView *picker = [[PickerThreeView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:esString(mm.ego) withDataArr:mm.frightens];
        [[UIApplication sharedApplication].keyWindow addSubview:picker];
        [picker showBottomView];
        picker.callBackModelArr = ^(NSArray * _Nonnull selArr) {
            FrightensModel *mod = [FrightensModel mj_objectWithKeyValues:selArr.firstObject];
            mm.mightn = esString(mod.cambridge);
            mm.risen = esString(mod.risen);
            [self.tableView reloadData];
        };
        
    }
    else if ([mm.weak isEqualToString:kMakemoneym]){

        NSArray *cityAry = [[NSUserDefaults standardUserDefaults] valueForKey:kCityDataAry];
    
        if (cityAry.count > 0) {
            PickerCityView *picker = [[PickerCityView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) withTitleStr:@"Select Address" withDataArr:cityAry];
            [[UIApplication sharedApplication].keyWindow addSubview:picker];
            [picker showBottomView];
            picker.callbackCityStr = ^(NSString * _Nonnull cityStr) {
                DLog(@"====%@",cityAry);
                mm.mightn = esString(cityStr);
                [self.tableView reloadData];
            };
        }
        
    }
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *sectionView = [[UIView alloc] init];
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001f;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footView = [[UIView alloc] init];
    return footView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.00001f;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)clickSubmitBtn{
    [self.view endEditing:YES];
    
    DLog(@"=====%@",self.data);
    
    NSMutableDictionary *muDic = [NSMutableDictionary dictionary];
    [muDic setValue:esString(self.idStr) forKey:@"gardens"];
    [muDic setValue:@"tattooingPersonal" forKey:@"tattooing"];
    
    for (int i=0; i<self.data.count; i++) {
        InPutModel *mm = [InPutModel mj_objectWithKeyValues:self.data[i]];
        if ([mm.weak isEqualToString:kMakemoneyk]) {
            [muDic setValue:esString(mm.risen) forKey:esString(mm.dim)];
        } else {
            [muDic setValue:esString(mm.mightn) forKey:esString(mm.dim)];
        }
    }
    
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    [BaseDataEngine apiForPath:kStorePersonalUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        [CreditServerHelper uploadMaiDianDataStartTimeStr:self.startTimeStr andEndTimeStr:[NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"] withTypeStr:@"5" withFirmly:esString(self.idStr) withSmaller:esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber])];
        [self enterNextPage];
        
//        [[NSNotificationCenter defaultCenter] postNotificationName:kNeedRefreshOrderDetNotification object:nil];
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
    
    
    
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)enterNextPage{
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    NSMutableDictionary *muDic = @{
        @"gardens":esString(self.idStr),
        @"tuesday":@"tuesdayOrderDet",
        @"needn":@"neednOrderDet",
    }.mutableCopy;
    [BaseDataEngine apiForPath:kProductDetUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        DLog(@"=====%@",responseDic);
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        
        NSDictionary *dic = responseDic[@"recollecting"];
       
        NSDictionary *lauDic = dic[@"laughing"];
        
        if (PMObjectIsEmpty(lauDic)) {
            [self enterOrderH5Page];
            return;
        }
        if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyf]) {
            [self goIdentifyVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyh]) {
            [self goWorkVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyi]) {
            [self goContactVertify];
        }
        else if ([esString(lauDic[@"tumbled"]) isEqualToString:kMakemoneyj]) {
            [self goBankVertify];
        }
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
    
}

- (void)enterOrderH5Page{
    NSString *stTime = [NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"];
    NSMutableDictionary *muDic = @{
        @"commissariat":esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber]),
        @"tracked":@"trackedIdStr",
        @"rot":@"rotIdStr",
        @"thrown":@"thrownIdStr",
        @"object":@"objectIdStr",
    }.mutableCopy;
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    [BaseDataEngine apiForPath:kByOrderIdGetUrl method:kPostMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }
        [CreditServerHelper uploadMaiDianDataStartTimeStr:stTime andEndTimeStr:[NSString getNowTimeTimestamp:@"yyyyMMdd HH:mm:ss"] withTypeStr:@"9" withFirmly:esString(self.idStr) withSmaller:esString([[NSUserDefaults standardUserDefaults] valueForKey:kOrderNumber])];
        NSDictionary *dic = responseDic[@"recollecting"];
        
        [[CreditCore globleCore] jumpToPage:esString(dic[@"exulting"])];
        
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:YES];
}

#pragma mark ----goIdentifyVertify
- (void)goIdentifyVertify{
    [CreaditUtilsHelper boxShowLoadWithMessage:@""];
    NSMutableDictionary *muDic = @{
        @"gardens":esString(self.idStr),
        @"loose":@"looseIdentify"
    }.mutableCopy;
    [BaseDataEngine apiForPath:kIDCardVertifyUrl method:kGetMethod parameter:muDic onCompleteSuccess:^(NSDictionary *responseDic) {
        [CreaditUtilsHelper hideLoadMessageBox];
        if ([responseDic[@"dim"] integerValue] != kSuccessCode) {
            [CreaditUtilsHelper boxShowErrorWithMessage:esString(responseDic[@"platform"]) duration:2.5f];
            return;
        }

        NSDictionary *dic = responseDic[@"recollecting"];
        
        IDVertifyModel *model = [IDVertifyModel mj_objectWithKeyValues:dic];
        
        
        if ([model.spluttering.teetotallers integerValue] == 0) {
            IDVertifyViewController *vc = [[IDVertifyViewController alloc] init];
            vc.ocrCardArr = model.wherever;
            vc.idStr = esString(self.idStr);
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        else if ([model.spluttering.teetotallers integerValue] == 1 && [model.abruptly integerValue] == 0 ){
            FaceViewController *vc = [[FaceViewController alloc] init];
            vc.cardStr = esString(model.spluttering.giggling);
            vc.idStr = esString(self.idStr);
            [self.navigationController pushViewController:vc animated:YES];
        }
        else if ([model.spluttering.teetotallers integerValue] == 1 && [model.abruptly integerValue] == 1){
            IDVerfitySuccessController *vc = [[IDVerfitySuccessController alloc] init];
            vc.cardStr = esString(model.spluttering.giggling);
            vc.nameStr = esString(model.spluttering.smoke.cambridge);
            vc.idNoStr = esString(model.spluttering.smoke.swallowed);
            vc.birDateStr = esString(model.spluttering.smoke.tightening);
            vc.idStr = esString(self.idStr);
            [self.navigationController pushViewController:vc animated:YES];
        }
        
    } onCompleteFailur:^(NSError *error) {
        [CreaditUtilsHelper hideLoadMessageBox];
    } withIsForm:NO];
}

#pragma mark ----workVertify
- (void)goWorkVertify{
    WorkInfoVertifyController *vc = [[WorkInfoVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----contactVertify
- (void)goContactVertify{
    ContactVertifyController *vc = [[ContactVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark ----bankVertify
- (void)goBankVertify{
    BankVertifyController *vc = [[BankVertifyController alloc] init];
    vc.idStr = esString(self.idStr);
    [self.navigationController pushViewController:vc animated:YES];
}

@end
